package com.travel.server.manager.impl.command.order;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.order.QueryOrderTravelDataRequest;
import com.travel.server.manager.dto.response.order.QueryOrderTravelDataResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.PageDto;
import com.travel.server.manager.impl.dal.TravelDal;
import com.travel.server.manager.impl.dal.TravelDal.TravelParam;
import com.travel.server.manager.model.OrderTravelInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/5 18:21
 * @Desc:
 */
@Component
@Slf4j
public class QueryOrderTravelDataCommand extends AbstractCommand<QueryOrderTravelDataRequest, QueryOrderTravelDataResponse> {

    @Autowired
    private TravelDal travelDal;

    @Override
    protected void checkInput(QueryOrderTravelDataRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public QueryOrderTravelDataResponse handle(QueryOrderTravelDataRequest request) {
        checkInput(request);

        TravelParam param = new TravelParam();
        param.setOrderId(request.getId());

        PageDto<OrderTravelInfo> pageDto = travelDal.select(param);

        return makeResponse(pageDto);
    }

    public QueryOrderTravelDataResponse makeResponse(PageDto<OrderTravelInfo> pageDto){
        QueryOrderTravelDataResponse response = new QueryOrderTravelDataResponse();

        List<OrderTravelInfo> list = pageDto.getList();
        if(!CollectionUtil.isEmpty(list)){
            OrderTravelInfo orderTravelInfo = list.get(0);
            response.setArriveFight(orderTravelInfo.getArriveFight());
            response.setEmergencyMobile(orderTravelInfo.getEmergencyMobile());
            response.setIdCard(orderTravelInfo.getIdCard());
            response.setInsuranceNo(orderTravelInfo.getInsuranceNo());
            response.setMobile(orderTravelInfo.getMobile());
            response.setName(orderTravelInfo.getName());
            response.setNeedPick(orderTravelInfo.getNeedPick());
            response.setPassport(orderTravelInfo.getPassport());
            response.setPickPlace(orderTravelInfo.getPlace());
            response.setPickTime(orderTravelInfo.getPickTime());
            response.setSex(orderTravelInfo.getSex());
        }

        return response;
    }
}
